# THIS SCRIPT ESTIMATES THE MODELS USED IN TABLE 2, ESTIMATING THE RELATIONSHIP
# BETWEEN MORAL TRADITIONALISM AND PARTISAN AFFECTIVE POLARIZATION, 
# CONDITIONAL ON MATCHING AND OTHER FORMS OF POLITICAL SOPHISTICATION.
# IT MUST BE USED IN CONJUNCTION WITH THE DATASETS "ANES2020_cleaned.csv" 
# AND "ANES1988.csv", ALSO AVAILABLE WITH THE REPLICATION MATERIALS ON DATAVERSE
rm(list = ls())
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)

# READ THE DATA
# 2020 ANES; USE THE FILE "ANES2020_cleaned.csv" 
data.2020 = read.csv(file.choose())
# 1988 ANES; USE THE FILE "ANES1988_cleaned.csv"
data.1988 = read.csv(file.choose())
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# DESIGN, 1988
design.1988 = svydesign(ids = ~1, 
                        weights = ~data.1988$weight,
                        data = data.1988)
# DESIGN, PRE-ELECTION MODULE, 2020
pre.design.2020 = svydesign(ids = ~1, 
                            weights = ~data.2020$V200010a,
                            data = data.2020)
# DESIGN, POST-ELECTION MODULE, 2020
post.design.2020 = svydesign(ids = ~1, 
                             weights = ~data.2020$V200010b[!is.na(data.2020$V200010b)],
                             data = subset(data.2020, !is.na(data.2020$V200010b)))
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# MODEL 1, 1988
model1.1988 = svyglm(tradvalues ~ repversusdemdiff_therm*programmatic,
                     design = design.1988)
summary(model1.1988)
# MODEL 1, 2020
model1.2020 = svyglm(tradvalues ~ repversusdemdiff_therm*programmatic,
                     design = post.design.2020)
summary(model1.2020)

# MODEL 2, 1988
model2.1988 = svyglm(tradvalues ~ repversusdemdiff_therm*programmatic + 
                       repversusdemdiff_therm*politicalknowledge,
                     design = design.1988)
summary(model2.1988)
# MODEL 2, 2020
model2.2020 = svyglm(tradvalues ~ repversusdemdiff_therm*programmatic + 
                       repversusdemdiff_therm*politicalknowledge,
                     design = post.design.2020)
summary(model2.2020)

# MODEL 3, 1988
model3.1988 = svyglm(tradvalues ~ repversusdemdiff_therm*programmatic + 
                       repversusdemdiff_therm*llactivism,
                     design = design.1988)
summary(model3.1988)
# MODEL 3, 2020
model3.2020 = svyglm(tradvalues ~ repversusdemdiff_therm*programmatic + 
                       repversusdemdiff_therm*llactivism,
                     design = post.design.2020)
summary(model3.2020)

# MODEL 4, 1988
model4.1988 = svyglm(tradvalues ~ repversusdemdiff_therm*programmatic + 
                       repversusdemdiff_therm*interest.politics,
                     design = design.1988)
summary(model4.1988)
# MODEL 4, 2020
model4.2020 = svyglm(tradvalues ~ repversusdemdiff_therm*programmatic + 
                       repversusdemdiff_therm*interest.politics,
                     design = post.design.2020)
summary(model4.2020)

# MODEL 5, 1988
model5.1988 = svyglm(tradvalues ~ repversusdemdiff_therm*programmatic + 
                       repversusdemdiff_therm*education,
                     design = design.1988)
summary(model5.1988)
# MODEL 5, 2020
model5.2020 = svyglm(tradvalues ~ repversusdemdiff_therm*programmatic + 
                       repversusdemdiff_therm*education,
                     design = post.design.2020)
summary(model5.2020)
